﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WarriorRush.GameDynamics;
using WarriorRush.UserInterface;
using WarriorRush.GameDynamics.Utilities;
namespace WarriorRush
{
    public class AiManager
    {
        public BattleState BattleState;
        public FrameState FrameState;
        private Random choiceGenerator;
        private int frameWait;
        private int framesElapsed;
        private bool isSelectionMade;


        public AiManager(BattleState battle, FrameState frame, int pauseDuration)
        {
            BattleState = battle;
            FrameState = frame;
            choiceGenerator = new Random();
            frameWait = pauseDuration;
            framesElapsed = 0;
            isSelectionMade = false;
        }

        public bool HandleSelection(SelectionHandler selection)
        {
            if (framesElapsed < frameWait)
            {
                framesElapsed++;
                return false;
            }
            

            if(!isSelectionMade)
            {
                MakeRandomSelection(selection);
                isSelectionMade = true;
                framesElapsed = 0;
                return false;
            }


            if (isSelectionMade)
            {
                FrameState.IsDoneHit = true;
                framesElapsed = 0;
                isSelectionMade = false;
                return true;
            }
            return false;

        }
        public void MakeRandomSelection(SelectionHandler selection)
        {
            
            var possibleSelections =  selection.Squares.Where(f => f.Square.HasWarrior);
            var choiceCount =possibleSelections.Count();
            var index = choiceGenerator.Next(0, choiceCount );
            possibleSelections.ElementAt(index).IsSelected = true;
        }
    }
}
